//
//  TTSCustomTabBarView.m
//  TTSVideo
//
//  Created by chenHR on 16/2/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "TTSCustomTabBarView.h"
#import "TTSCustomTabBarButton.h"

@interface TTSCustomTabBarView ()
/**
 *  记录选中的按钮
 */
@property (nonatomic, weak) UIButton *selecterBtn;

@end

@implementation TTSCustomTabBarView

- (void)TTSAddButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selecterImage{
    TTSCustomTabBarButton *btn  = [[TTSCustomTabBarButton alloc] init];

    [btn setImage:image forState:UIControlStateNormal];
    [btn setImage:selecterImage forState:UIControlStateSelected];
    
    [self addSubview:btn];
    
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    // 选中第一个按钮
    if (self.subviews.count == 1) {
        [self buttonAction:btn];
    }
    
    
    
}

- (void)TTSAddButtonWithTitle:(NSString *)title color:(UIColor *)color selecterColor:(UIColor *)selecterColor{
    TTSCustomTabBarButton *btn = [[TTSCustomTabBarButton alloc] init];
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setTitleColor:selecterColor forState:UIControlStateSelected];
    [self addSubview:btn];
    [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    if (self.subviews.count == 1) {
        [self buttonAction:btn];
    }
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(btn.frame.size.width, btn.frame.origin.y, 1, btn.frame.size.height-4)];
//    line.backgroundColor = [UIColor blackColor];
//    NSLog(@"%f", line.frame.origin.x);
//    [self addSubview:line];

    
}

/**
 *  按钮点击事件
 *
 *  @param btn 需要监听的按钮
 */
- (void)buttonAction:(TTSCustomTabBarButton *)btn{
    // 将之前的按钮设置为未选中
    self.selecterBtn.selected = NO;
    // 当前按钮选中
    btn.selected = YES;
    // 记录当前的按钮
    self.selecterBtn = btn;
    
    // 判断代理方法是否实现
    if ([self.tabBarDelegate respondsToSelector:@selector(TTSTabBar:selectedFromFlag:toFlag:)]) {
        [self.tabBarDelegate TTSTabBar:self selectedFromFlag:self.selecterBtn.tag toFlag:btn.tag];
    }
    
    
    
}

/**
 *  自动布局
 */
- (void)layoutSubviews{
    [super layoutSubviews];
    NSInteger count = self.subviews.count;
    for (int i = 0; i < count; i++) {
        TTSCustomTabBarButton *btn = self.subviews[i];
        btn.tag = i;
        
        CGFloat x = i * self.bounds.size.width / count;
        CGFloat y = 0;
        CGFloat width = self.bounds.size.width / count;
        CGFloat height = self.bounds.size.height;
        btn.frame = CGRectMake(x, y, width, height);
        if (count - 1 > i) {
//            UIView *line = [[UIView alloc] init];
//            line.backgroundColor = [UIColor lightGrayColor];
//            line.frame = CGRectMake(btn.frame.size.width - 1, btn.frame.origin.y, 1, btn.frame.size.height-5);
            [btn setBackgroundImage:[UIImage imageNamed:@"line1"] forState:UIControlStateNormal];
            
        }
        
    }
    
    
}


@end
